//
//  YQArticleListViewController.m
//  iyuanqi
//
//  Created by czl on 2017/7/28.
//  Copyright © 2017年 czl. All rights reserved.
//

#import "YQArticleListViewController.h"
#import "SHSegmentedControlTableView.h"
#import "YQArticleListHeader.h"
#import "YQArticleListTable.h"
#import "YQArticleDetailViewController.h"

@interface YQArticleListViewController ()
<SHSegTableViewDelegate>

@property(nonatomic,copy)YQForumObject *forumObject;

@property(nonatomic,strong)SHSegmentedControlTableView *segTableView;
@property(nonatomic,strong)SHSegmentControl *segmentControl;
@property(nonatomic,strong)YQArticleListHeader *headerView;

@property(nonatomic,strong)YQArticleListTable* tabNew;
@property(nonatomic,strong)YQArticleListTable* tabHot;
@property(nonatomic,strong)YQArticleListTable* tabReply;

@end

@implementation YQArticleListViewController

- (instancetype)initWithFid:(YQForumObject*)forumObject {
    self = [super init];
    if (self) {
        self.forumObject = forumObject;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.forumObject.name;
    [self.segTableView setTableViews:@[self.tabNew, self.tabHot, self.tabReply]];
    
    [self.view addSubview:self.segTableView];
    [self.segTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    self.headerView.forumObject = self.forumObject;
    WeakSelf;
    self.segmentControl.curClick = ^(NSInteger index) {
        [weakSelf.segTableView setSegmentSelectIndex:index];
    };
    [self getArticleList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getArticleList {
    [self.tabNew getArticleList];
    [self.tabHot getArticleList];
    [self.tabReply getArticleList];
}

- (void)toArticleDetail:(YQArticleObject*)object {
    YQArticleDetailViewController* vc = [[YQArticleDetailViewController alloc]initWithArticle:object];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
#pragma mark   ==============SHSegTableViewDelegate==============
- (void)segTableViewDidScrollY:(CGFloat)offsetY {
    
}

- (void)segTableViewDidScroll:(UIScrollView *)tableView {
    
}

- (void)segTableViewDidScrollSub:(UIScrollView *)subTableView {
    
}

- (void)segTableViewDidScrollProgress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
    if (progress == 1) {
        [self.segmentControl setSegmentSelectedIndex:targetIndex];
    }
}

- (SHSegmentedControlTableView *)segTableView{
    if(!_segTableView){
        _segTableView = ({
            SHSegmentedControlTableView * object = [[SHSegmentedControlTableView alloc] initWithFrame:self.view.bounds];
            object.delegateCell = self;
            [object setTopView:self.headerView];
            [object setBarView:self.segmentControl];
            object;
       });
    }
    return _segTableView;
}

- (SHSegmentControl *)segmentControl{
    if(!_segmentControl){
        _segmentControl = ({
            SHSegmentControl * object = [[SHSegmentControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) items:@[@"最新",@"最热",@"最近回复"]];
            object.titleSelectColor = MainColor;
            object.progressColor = MainColor;
            [object reloadViews];
            WeakSelf;
            _segmentControl.curClick = ^(NSInteger index) {
                [weakSelf.segTableView setSegmentSelectIndex:index];
            };
            object;
       });
    }
    return _segmentControl;
}

- (YQArticleListHeader *)headerView{
    if(!_headerView){
        _headerView = ({
            YQArticleListHeader * object = [[YQArticleListHeader alloc]initWithFrame:CGRectMake(0, 0, 0, 120)];
            object;
       });
    }
    return _headerView;
}

- (YQArticleListTable *)tabNew{
    if(!_tabNew){
        _tabNew = ({
            YQArticleListTable * object = [[YQArticleListTable alloc]initWithType:1 fid:self.forumObject.fid];
            object;
       });
    }
    return _tabNew;
}

- (YQArticleListTable *)tabHot{
    if(!_tabHot){
        _tabHot = ({
            YQArticleListTable * object = [[YQArticleListTable alloc]initWithType:2 fid:self.forumObject.fid];
            object;
       });
    }
    return _tabHot;
}

- (YQArticleListTable *)tabReply{
    if(!_tabReply){
        _tabReply = ({
            YQArticleListTable * object = [[YQArticleListTable alloc]initWithType:3 fid:self.forumObject.fid];
            object;
       });
    }
    return _tabReply;
}
@end
